package com.modelclient.app.test;

import android.view.LayoutInflater;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.modelclient.app.R;
import com.modelclient.baselib.model.MineDataBean;
import com.modelclient.baselib.util.StatusBarUtils;
import com.modelclient.baselib.view.BaseFragment;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;

import butterknife.BindView;
import butterknife.OnClick;

public class MineFragment extends BaseFragment<MinePresenter> implements MineContract.View {

    public static String TAG = "MineFragment";

    private ImageView ivHead;
    private TextView tvName;
    private TextView tvPhone;
    private FrameLayout viewHead;
    private RecyclerView recyclerView;
    private SmartRefreshLayout swipeLayout;
    private LinearLayout viewNoData;

    private int mPosition = 0;
    private MineMeetAdapter mineMeetAdapter;
    private MineDataBean mineDataBean;

    public static MineFragment newInstance() {
        return new MineFragment();
    }

    @Override
    protected View getRootView(LayoutInflater inflater) {
        return inflater.inflate(R.layout.fragment_mine, null);
    }

    @Override
    protected void initOnlyOnce() {
        super.initOnlyOnce();
    }

    @Override
    protected void initView(View mRootView) {
        super.initView(mRootView);

        ivHead = mRootView.findViewById(R.id.iv_head);
        tvName = mRootView.findViewById(R.id.tv_name);
        tvPhone = mRootView.findViewById(R.id.tv_phone);
        viewHead = mRootView.findViewById(R.id.view_head);
        recyclerView = mRootView.findViewById(R.id.recyclerView);
        swipeLayout = mRootView.findViewById(R.id.swipeLayout);
        viewNoData = mRootView.findViewById(R.id.view_no_data);

        //2.设置状态栏
        StatusBarUtils.immersive(getActivity());
        StatusBarUtils.setPaddingSmart(getActivity(), viewHead);

//        swipeLayout.setRefreshHeader(new MyRefreshHead(getActivity()));
        swipeLayout.setOnRefreshListener(refreshLayout -> {
            mPresenter.getCustomerInfo();
        });

        mPresenter.getCustomerInfo();
    }

    @Override
    public void onResume() {
        super.onResume();
    }

    @OnClick({R.id.tv_exit_login, R.id.view_code})
    void onClick(View view) {
        switch (view.getId()) {
            case R.id.tv_exit_login:
                mPresenter.logout();
                break;
            case R.id.view_code:
                if (mineDataBean != null) {
//                    CommonUtil.goAnyWhere(mContext,mineDataBean.cardUrl,"","","","","");
                }
                break;
        }
    }

    @Override
    public void getCustomerInfoSuccess(MineDataBean mineDataBean) {
        if (swipeLayout != null) {
            swipeLayout.finishRefresh();
        }
        this.mineDataBean = mineDataBean;
        tvName.setText(mineDataBean.nickname);
        tvPhone.setText(mineDataBean.mobile);
//        ViewTransformUtil.glideImageView(getActivity(), mineDataBean.headImgUrl,ivHead,
//                new CropCircleWithBorderTransformation(0,0x0A3D2529), R.mipmap.icon_mine_head);

        if (mineDataBean.subscribeEvents != null && mineDataBean.subscribeEvents.size() > 0) {
            swipeLayout.setVisibility(View.VISIBLE);
            viewNoData.setVisibility(View.GONE);
            recyclerView.setVisibility(View.VISIBLE);
            recyclerView.setLayoutManager(new LinearLayoutManager(mContext));
            mineMeetAdapter = new MineMeetAdapter(mineDataBean.subscribeEvents);
            mineMeetAdapter.setOnItemClickListener((adapter, view, position) -> {
                MineDataBean.HotEventItem item = mineDataBean.subscribeEvents.get(position);
//                CommonUtil.goAnyWhere(mContext,item.eventUrl,"","","","","");
            });
            mineMeetAdapter.setOnClickStatusListener((eventId, position) -> {
                mPosition = position;
                mPresenter.cancelSubscribe(eventId);
            });
            mineMeetAdapter.bindToRecyclerView(recyclerView);
        } else {
            swipeLayout.setVisibility(View.GONE);
            viewNoData.setVisibility(View.VISIBLE);
        }
    }

    @Override
    public void getCustomerInfoFailed(String msg) {
        if (swipeLayout != null) {
            swipeLayout.finishRefresh(false);
        }
        showToast(msg);
    }

    @Override
    public void logout() {
//        UserDataManager.getInstance().clearLoginInfo();
//        ARouter.getInstance().build(AroutePath.MAIN_ACTIVITY).withString("position","0").navigation();
    }

    @Override
    public void logoutFailed(String msg) {
        showToast(msg);
    }

    @Override
    public void cancelSuccess() {
        mPresenter.getCustomerInfo();
    }

    @Override
    public void cancelFailed(String msg) {
        showToast(msg);
    }

    @Override
    public void onHiddenChanged(boolean hidden) {
        super.onHiddenChanged(hidden);
        mPresenter.getCustomerInfo();
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
    }
}
